"""
邮件发送相关的Pydantic模式
"""
from typing import Optional, List, Dict, Any
from pydantic import BaseModel, EmailStr
from datetime import datetime
from models.email_task import EmailTaskStatus, EmailSendMode, EmailSendStatus


class EmailRecipient(BaseModel):
    """邮件收件人"""
    email: EmailStr
    name: Optional[str] = None
    variables: Optional[Dict[str, Any]] = None


class EmailSendRequest(BaseModel):
    """邮件发送请求"""
    template_id: Optional[int] = None
    email_config_id: int
    recipients: List[EmailRecipient]
    subject: Optional[str] = None
    content: Optional[str] = None
    template_variables: Optional[Dict[str, Any]] = None
    send_mode: EmailSendMode = EmailSendMode.BATCH
    send_immediately: bool = True
    scheduled_time: Optional[datetime] = None
    attachments: Optional[List[Dict[str, Any]]] = None


class EmailSendResponse(BaseModel):
    """邮件发送响应"""
    success: bool
    message: str
    task_id: Optional[str] = None
    sent_count: Optional[int] = None
    failed_count: Optional[int] = None
    details: Optional[Dict[str, Any]] = None


class EmailTaskBase(BaseModel):
    """邮件任务基础模式"""
    subject: str
    content: str
    template_id: Optional[int] = None
    email_config_id: int
    data_source_id: Optional[int] = None
    send_mode: EmailSendMode = EmailSendMode.BATCH
    send_immediately: bool = True
    scheduled_time: Optional[datetime] = None
    recipients_config: Optional[Dict[str, Any]] = None
    template_variables: Optional[Dict[str, Any]] = None
    attachments: Optional[List[Dict[str, Any]]] = None


class EmailTaskCreate(EmailTaskBase):
    """创建邮件任务模式"""
    pass


class EmailTaskUpdate(BaseModel):
    """更新邮件任务模式"""
    status: Optional[EmailTaskStatus] = None
    error_message: Optional[str] = None
    sent_count: Optional[int] = None
    failed_count: Optional[int] = None
    started_at: Optional[datetime] = None
    completed_at: Optional[datetime] = None


class EmailTask(EmailTaskBase):
    """邮件任务响应模式"""
    id: str
    recipients_count: int
    sent_count: int
    failed_count: int
    status: EmailTaskStatus
    error_message: Optional[str] = None
    created_at: datetime
    started_at: Optional[datetime] = None
    completed_at: Optional[datetime] = None
    user_id: int

    class Config:
        from_attributes = True


class EmailTaskList(BaseModel):
    """邮件任务列表"""
    items: List[EmailTask]
    total: int
    page: int
    size: int
    pages: int


class EmailSendRecordBase(BaseModel):
    """邮件发送记录基础模式"""
    task_id: str
    recipient_email: EmailStr
    recipient_name: Optional[str] = None
    rendered_subject: Optional[str] = None
    rendered_content: Optional[str] = None
    recipient_variables: Optional[Dict[str, Any]] = None


class EmailSendRecordCreate(EmailSendRecordBase):
    """创建邮件发送记录模式"""
    pass


class EmailSendRecordUpdate(BaseModel):
    """更新邮件发送记录模式"""
    status: Optional[EmailSendStatus] = None
    sent_at: Optional[datetime] = None
    error_message: Optional[str] = None
    message_id: Optional[str] = None


class EmailSendRecord(EmailSendRecordBase):
    """邮件发送记录响应模式"""
    id: str
    status: EmailSendStatus
    sent_at: Optional[datetime] = None
    error_message: Optional[str] = None
    message_id: Optional[str] = None
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class EmailSendHistoryList(BaseModel):
    """邮件发送历史列表"""
    items: List[EmailSendRecord]
    total: int
    page: int
    size: int
    pages: int


class EmailStatistics(BaseModel):
    """邮件统计信息"""
    total_tasks: int
    total_sent: int
    total_failed: int
    success_rate: float
    today_sent: int
    this_week_sent: int
    this_month_sent: int
    recent_tasks: List[EmailTask]


class ContactImportRequest(BaseModel):
    """联系人导入请求"""
    data_source_id: int
    table_name: Optional[str] = None
    email_column: str
    name_column: Optional[str] = None
    additional_columns: Optional[List[str]] = None
    filters: Optional[Dict[str, Any]] = None


class ContactImportResponse(BaseModel):
    """联系人导入响应"""
    success: bool
    message: str
    imported_count: int
    failed_count: int
    contacts: List[EmailRecipient]


class EmailPreviewRequest(BaseModel):
    """邮件预览请求"""
    template_id: Optional[int] = None
    subject: Optional[str] = None
    content: Optional[str] = None
    template_variables: Optional[Dict[str, Any]] = None
    recipient: Optional[EmailRecipient] = None


class EmailPreviewResponse(BaseModel):
    """邮件预览响应"""
    rendered_subject: str
    rendered_content: str
    html_content: str
    variables_used: Optional[List[str]] = None
    missing_variables: Optional[List[str]] = None
